<?php
require_once "includes/functions.inc.php";

function is_valid_email($email) 
{
  return eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4})$", $email);
}

if (ctype_alnum($_POST['username']) && ctype_alnum($_POST['password']) && strlen($_POST['username']) > 3 && strlen($_POST['password']) > 3 && ctype_alpha($_POST['user_first_name'])	&& strlen($_POST['user_first_name']) > 2 	&& ctype_alpha($_POST['user_last_name']) && strlen($_POST['user_last_name']) > 2 && strlen($_POST['email']) > 6 && strlen($_POST['x']) > 0 && strlen($_POST['y']) > 0)	
{
    if (!is_valid_email($_POST['email']))
        die("7");

    require "includes/db.php";

    $username = mysql_real_escape_string($_POST['username']);
    $password = substr(md5("bla" . mysql_real_escape_string($_POST['password'])), 1);
    $user_first_name = mysql_real_escape_string($_POST['user_first_name']);
    $user_last_name = mysql_real_escape_string($_POST['user_last_name']);
    $email = mysql_real_escape_string($_POST['email']);
    $x = $_POST['x'];
    $y = $_POST['y'];

    $sql = "SELECT `user_id` FROM `users` WHERE `user_name` LIKE '" . $username . "' LIMIT 1;";
    $result = mysql_query($sql);

    if (!$result) 
    {
        die("2"); // technische storing
    }

    if (mysql_num_rows($result) != 0) 
    {
        die("4"); // gebruiker bestaat al
    }

    $sql = "INSERT INTO `users` (`user_name` ,`user_first_name` ,`user_last_name` ,`user_email` ,`user_pass` ,`x` ,`y`)VALUES (
                            '" . $username . "', 
                            '" . $user_first_name . "', 
                            '" . $user_last_name . "', 
                            '" . $email . "', 
                            '" . $password . "', 
                            '" . $x . "', 
                            '" . $y . "');";

    $result = mysql_query($sql);
    
    if (!$result) 
    {
        die("2"); // SQL fout
    }

    echo "3"; // gebruiker aangemaakt! :D
} 
else 
{
    echo "1"; // gegevens klopten niet
}
?>